/**
 * 
 */
package com.htht.smartschool.service.license;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.transform.Transformers;

import com.htht.smartschool.common.service.BaseService;
import com.htht.smartschool.model.datarole.DataBean;
import com.htht.smartschool.model.datarole.DataRole;
import com.htht.smartschool.model.datauser.DataUser;
import com.htht.smartschool.model.license.License;
import com.htht.smartschool.model.models.Models;
import com.htht.smartschool.model.roles.Role;
import com.htht.smartschool.model.user.TUser;
import com.htht.smartschool.util.system.EncryptUtil;
import com.htht.smartschool.util.system.GsonUtils;

/**
 * @author wuqiwei
 *
 */
public class LicenseService extends BaseService{

	/**获取角色信息
	 * @author:wuqiwei
	 * @param @param role
	 * @param @return
	 * @date:2016年2月18日上午10:41:27
	*/
	public  Map getRole(Role role) {
		Map map = new HashMap();
		String  sql = "SELECT ts.schoolname,ts.schoolcode,tr.code,tr.name,tr.describe,tr.remark,tr.id FROM t_role tr, (SELECT NAME AS schoolname,schoolcode FROM t_school WHERE schoolcode = :schoolcode) ts WHERE tr.id >= '1' AND tr.schoolcode = ts.schoolcode";
		if(role.getSchoolcode() != null && role.getSchoolcode().length() >0){
			sql += " and tr.schoolcode = :schoolcode";
		}
		if(role.getCode() != null && role.getCode().length() > 0){
			sql += " and tr.code = :code";
		}
		if(role.getName() != null && role.getName().length() > 0){
			sql += " and tr.name like :name";
			role.setName("%"+role.getName()+"%");
		}
		if(role.getDescribe() != null && role.getDescribe().length() >0){
			sql += " and tr.describe= :describe";
		}
		if(role.getRemark() != null && role.getRemark().length() >0){
			sql += " and tr.remark = :remark";
		}
		List<Object[]> list = null;
		if(role.getCurrentpage() != null && role.getPagesize() != null){
			 list = getDao().queryBySqlPage(sql,role,role.getCurrentpage(),role.getPagesize()); 
		}else {
			 list = getDao().queryBySQL(sql,role);
		}
		String sql2 = "select count(*) from ("+ sql +") t";
		int counts = 0;
		counts = getDao().getCountBySqlPage(sql2,role);
		List<Role> result = new ArrayList<Role>();
		for (Object[] obj : list) {
			Role datarole = new Role();
			datarole.setSchoolname(String.valueOf(obj[0]));
			datarole.setSchoolcode(String.valueOf(obj[1]));
			datarole.setCode(String.valueOf(obj[2]));
			datarole.setName(String.valueOf(obj[3]));
			datarole.setDescribe(String.valueOf(obj[4]));
			datarole.setRemark(String.valueOf(obj[5]));
			datarole.setId(Long.valueOf(String.valueOf(obj[6])));
			result.add(datarole);
		}
		map.put("data", result);
		map.put("total", counts);
		return map;
	}

	/**修改角色信息
	 * @author:wuqiwei
	 * @param @param role
	 * @param @return
	 * @date:2016年2月18日上午10:41:32
	*/
	public  boolean updateRole(Role role) {
		String sql = "update t_role t set t.schoolcode = :schoolcode,t.code = :code,t.name = :name,t.describe = :describe,t.remark = :remark where t.id = :id";
		int a = getDao().executeSQL(sql,role);
		if(a == -1){
			return false;
		}else{
			return true;
		}
	}

	/**删除表t_role信息
	 * @author:wuqiwei
	 * @param @param role
	 * @param @return
	 * @date:2016年2月18日上午10:57:28
	*/
	public boolean deleteRole(Role role) {
		boolean flag = false;
		String sql = "delete from t_role where id = :id";
		int a = getDao().executeSQL(sql,role);
		if (a >= 0) {
			flag = true;
		}
		return flag;
	}

	/**添加角色t_role信息
	 * @author:wuqiwei
	 * @param @param role
	 * @param @return
	 * @date:2016年2月18日上午11:01:22
	*/
	public boolean insertRole(Role role) {
		boolean flag = false;
		String idsql = "SELECT MAX(id) FROM t_role ";
		List list = getDao().queryBySQL(idsql);
		String i =  String.valueOf(list.get(0));
		if(i == null || i.equalsIgnoreCase("null")){
			i = "1";
		}else{
			i = String.valueOf(Integer.parseInt(i) + 1);
		}
		String sql1 = "select * from t_role where name = :name and schoolcode = :schoolcode";
		List b = getDao().queryBySQL(sql1,role);
		if( b.size() > 0){
			flag = false;
		}else{
			String sql =" insert into t_role values(null,:schoolcode,:code,:name,:describe,:remark)";
			role.setCode(i);
			int a = getDao().executeSQL(sql,role);
			 if(a >= 0){
				 flag = true;
			 }
		}
		return flag;
	}

	/**插入权限模块信息
	 * @author:wuqiwei
	 * @param @param license
	 * @param @param models
	 * @param @return
	 * @date:2016年2月18日上午11:17:33
	*/
	public boolean insertLicense(License license) {
		String sql = "insert into t_license  values(null,:rolecode,:modular)";
		int a = getDao().executeSQL(sql,license);
		return a == -1 ? false :true;
	}

	/**查看权限表
	 * @author:wuqiwei
	 * @param @param license
	 * @param @return
	 * @date:2016年2月18日上午11:25:27
	*/
	public List<License> getLicense(License license) {
		String sql = "select * FROM t_license where id >= '1'";
		if(license.getRolecode() != null && license.getRolecode().length() >0){
			license.setRolecode(license.getRolecode().replace(",", "^"));
			sql += " and rolecode in (:rolecode)";
		}
		if(license.getModular() != null && license.getModular().length() >0){
			sql += " and modular = :modular";
		}
	
		List<Object[]> list = getDao().queryBySQL(sql,license); 
		List<License> result = new ArrayList<License>();
		for(Object[] obj : list){
			License licensedata = new License();
			licensedata.setId(Long.valueOf(String.valueOf(obj[0])));
			licensedata.setRolecode(String.valueOf(obj[1]));
			licensedata.setModular(String.valueOf(obj[2]));
			result.add(licensedata);
		}
		return result;
	}

	/**修改权限表
	 * @author:wuqiwei
	 * @param @param license
	 * @param @return
	 * @date:2016年2月18日下午1:24:31
	*/
	public boolean updateLicense(License license) {
		String sql = "update  t_license  set rolecode = :rolecode,modular = :modular where id = :id";
		int a = getDao().executeSQL(sql,license);
		if(a == -1){
			return false;
		}else{
			return true;
		}
	}

	/**删除权限信息
	 * @author:wuqiwei
	 * @param @param license
	 * @param @return
	 * @date:2016年2月18日下午1:31:26
	*/
	public boolean deleteLicense(License license) {
		boolean flag = false;
		String sql = "delete from t_license where id = :id";
		int a = getDao().executeSQL(sql,license);
		if (a >= 0) {
			flag = true;
		}
		return flag;
	}

	/**获取用户数据访问限制表信息
	 * @author:wuqiwei
	 * @param @param datauser
	 * @param @return
	 * @date:2016年2月18日下午1:45:41
	*/
	public List<DataUser> getDataUser(DataUser datauser) {
		String sql = " select * from t_data_user where id >= '1'";
		if(datauser.getUsercode() != null && datauser.getUsercode().length() > 0){
			sql +=" and usercode = :usercode";
		}
		if(datauser.getClasscode() != null && datauser.getClasscode().length() >0){
			sql += " and classcode = :classcode";
		}
		if(datauser.getGrade() != null && datauser.getGrade().length() > 0){
			sql +=" and grade = :grade";
		}
		if(datauser.getTheclass() != null && datauser.getTheclass().length() >0){
			sql +=" and class = :theclass";
		}
		if(datauser.getRemark() != null && datauser.getRemark().length() >0){
			sql +=" and remark = :remark";
		}
		List<Object[]> list = getDao().queryBySQL(sql,datauser); 
		List<DataUser> result = new ArrayList<DataUser>();
		for(Object[] obj : list){
			DataUser user = new DataUser();
			user.setId(Long.valueOf(String.valueOf(obj[0])));
			user.setUsercode(String.valueOf(obj[1]));
			user.setClasscode(String.valueOf(obj[2]));
			user.setGrade(String.valueOf(obj[3]));
			user.setTheclass(String.valueOf(obj[4]));
			user.setRemark(String.valueOf(obj[5]));
			result.add(user);
		}
		return result;
	}

	/**修改用户数据访问限制表信息
	 * @author:wuqiwei
	 * @param @param datauser
	 * @param @return
	 * @date:2016年2月18日下午1:58:02
	*/
	public boolean updateDataUser(DataUser datauser) {
		String sql = " update t_data_user set usercode = :usercode,classcode = :classcode,grade = :grade,class = :class,remark = :remark where id = :id";
		int a = getDao().executeSQL(sql,datauser);
		if(a == -1){
			return false;
		}else{
			return true;
		}
	}

	/**删除用户数据访问限制表信息
	 * @author:wuqiwei
	 * @param @param datauser
	 * @param @return
	 * @date:2016年2月18日下午2:13:35
	*/
	public boolean deleteDataUser(DataUser datauser) {
		boolean flag = false;
		String sql = " delete from t_data_user where id = :id";
		int a = getDao().executeSQL(sql,datauser);
		if (a >= 0) {
			flag = true;
		}
		return flag;
	}

	/**添加用户访问限制信息
	 * @author:wuqiwei
	 * @param @param datauser
	 * @param @return
	 * @date:2016年2月18日下午2:16:10
	*/
	public boolean insertDataUser(DataUser datauser) {
		String sql = " insert into t_data_user values(null,:usercode,:classcode,:grade,:theclass,:remark)";
		int a = getDao().executeSQL(sql,datauser);
		return a == -1 ? false :true;
	}

	/**查询角色数据访问限制表信息
	 * @author:wuqiwei
	 * @param @param datarole
	 * @param @return
	 * @date:2016年2月18日下午2:22:27
	*/
	@SuppressWarnings("unchecked")
	public List<DataBean> getDataRole(DataRole datarole) {
		String sql =" select td.id,td.rolecode,GROUP_CONCAT(td.classcode),td.grade,GROUP_CONCAT(td.classes),td.remark,tc.type from t_data_role td,t_class tc where td.id >= '1' and tc.classcode = td.classcode";
		if(datarole.getRolecode() != null && datarole.getRolecode().length()>0){
			datarole.setRolecode(datarole.getRolecode().replace(",", "^"));
			sql += " and td.rolecode in (:rolecode)";
		}
		if(datarole.getType() != null && datarole.getType().length() > 0){
			sql += " AND tc.type = :type";
		}
		if(datarole.getClasscode() != null && datarole.getClasscode().length() >0){
			sql += " and GROUP_CONCAT(td.classcode) = :classcode";
		}
		if(datarole.getGrade() != null && datarole.getGrade().length() >0){
			sql += " and td.grade = :grade";
		}
		if(datarole.getClasses() != null && datarole.getClasses().length() >0){
			sql += " and GROUP_CONCAT(td.classes) = :classes";
		}
		if(datarole.getRemark() != null && datarole.getRemark().length() >0){
			sql += " and td.remark = :remark";
		}
			sql += " GROUP BY td.rolecode,td.grade,tc.type ORDER BY tc.type, td.grade*1";
		List<Object[]> list = getDao().queryBySQL(sql,datarole);
		List<DataBean> result = new ArrayList<DataBean>();
		List<DataRole> data = new ArrayList<DataRole>();
		int j = 0;
		for(Object[] obj :list){
			DataRole role = new DataRole();
			DataBean databean = new DataBean();
			String type = String.valueOf(obj[6]);
			role.setId(Long.valueOf(String.valueOf(obj[0])));
			role.setRolecode(String.valueOf(obj[1]));
			role.setClasscode(String.valueOf(obj[2]));
			role.setGrade(String.valueOf(obj[3]));
			role.setClasses(String.valueOf(obj[4]));
			role.setRemark(String.valueOf(obj[5]));
			
			if(j == 0){
				databean.setType(type);
				databean.setData(data);
				result.add(databean);
				j++;
			}else{
				if(type.equals(result.get(j-1).getType())){
					databean.setData(data);
				}else{
					data = new ArrayList<DataRole>();
					databean.setType(type);
					databean.setData(data);
					result.add(databean);
					j++;
				}
			}
			data.add(role);
		}
		return result;
	}

	/**修改角色数据访问限制表信息
	 * @author:wuqiwei
	 * @param @param datarole
	 * @param @return
	 * @date:2016年2月18日下午2:31:53
	*/
	public boolean updateDataRole(DataRole datarole) {
		String sql = "update t_data_role set rolecode = :rolecode,classcode = :classcode,grade = :grade,class = :class,remark = :remark where id = :id";
		int a = getDao().executeSQL(sql,datarole);
		if(a == -1){
			return false;
		}else{
			return true;
		}
	}

	/**删除角色数据访问限制表信息
	 * @author:wuqiwei
	 * @param @param datarole
	 * @param @return
	 * @date:2016年2月18日下午2:35:38
	*/
	public boolean deleteDataRole(DataRole datarole) {
		boolean flag = false;
		String sql = "delete from t_data_role where id = :id";
		int a = getDao().executeSQL(sql,datarole);
		if (a >= 0) {
			flag = true;
		}
		return flag;
	}

	/**插入角色数据访问限制表信息
	 * @author:wuqiwei
	 * @param @param datarole
	 * @param @return
	 * @date:2016年2月18日下午2:37:47
	*/
	public boolean insertDataRole(DataRole datarole) {
		String sql = " insert into t_data_role  values(null,:rolecode,:classcode,:grade,:classes,:remark)";
		int a = getDao().executeSQL(sql,datarole);
		return a == -1 ? false :true;
	}

	/**查询角色权限和模块信息
	 * @author:wuqiwei
	 * @param @param userlicenseModel
	 * @param @return
	 * @date:2016年2月18日下午3:23:25
	*/
	public List<Models> queryUserlicenseModel(
			Models models) {
		String sql = "SELECT tm.value,tm.url,tm.icon,tm.code FROM t_role ts,t_license tl,t_models tm ";
		if(models.getUsercode() != null && models.getUsercode().length() > 0 && models.getUsertype() != null){
			models.setUserkey(EncryptUtil.encryptMd5Password2(models.getUsercode()+models.getUsertype()));
			sql += " ,t_role_user tr where tr.userkey = '"+models.getUserkey()+"' and tr.rolecode = ts.code";
		}
		if(models.getCode() != null && models.getCode().length() > 0){
			sql += " where tl.rolecode = '"+models.getCode()+"'";
		}
		sql += " AND tl.rolecode = ts.code AND tl.modular = tm.code GROUP BY tm.code ORDER BY tm.code ASC";
		List<Models> list = getDao().getSession().createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(Models.class)).list();
		List<Models> result = new ArrayList<Models>(); 
		for (int i = 0; i < list.size(); i++) {
			Models tm = list.get(i);
			String code = tm.getCode();
			List<Models> children = getChildren(list,code,i+1);//修改子级配置级别分割字节数
			if(children.size()>0){
				tm.setChildren(children);
			}
			//修改配置级别分隔字节数
			if(code.length() == 2){
				result.add(tm);
			}
		}
		return result;
	}

	/**查询模块信息
	 * @author:wuqiwei
	 * @param @param models
	 * @param @return
	 * @date:2016年2月18日下午5:03:54  
	*/
	@SuppressWarnings("unchecked")
	public List<Models> queryModels() {
		String firsql = "SELECT value,url,icon,code FROM t_models GROUP BY code ORDER BY code ASC";
		List<Models> list = getDao().getSession().createSQLQuery(firsql).setResultTransformer(Transformers.aliasToBean(Models.class)).list();
		getDao().closeSession();
		List<Models> result = new ArrayList<Models>(); 
		for (int i = 0; i < list.size(); i++) {
			Models tm = list.get(i);
			String code = tm.getCode();
			List<Models> children = getChildren(list,code,i+1);//修改子级配置级别分割字节数
			if(children.size()>0){
				tm.setChildren(children);
			}
			//修改配置级别分隔字节数
			if(code.length() == 2){
				result.add(tm);
			}
		}
		return result;
	}
	/**
	 * 修改子级配置级别分割字节数
	 * @author:wuqiwei
	 * @param @param list
	 * @param @param code
	 * @param @param n
	 * @param @return
	 * @date:2016年2月22日下午2:19:53
	 */
	public static List<Models> getChildren(List<Models> list,String code,int n){
		List<Models> children = new ArrayList<Models>();
		for (int i = n; i < list.size(); i++) {
			Models tm = list.get(i);
			//修改配置级别分隔字节数
			if(tm.getCode().length() > code.length() && tm.getCode().substring(0, code.length()).equalsIgnoreCase(code) && code.length() != tm.getCode().length()
				&& (tm.getCode().length() - code.length()) <= 2){
				children.add(tm);
			}
		}
		return children;
	}


	/**批量修改角色用户信息
	 * @author:wuqiwei
	 * @param @param role
	 * @param @return
	 * @date:2016年2月19日上午10:50:17
	*/
	public boolean updateRoleUser(Role role) {
		boolean flag = false;
		String value = "";
		String values = "";
		List list = role.getRolecodes();
		List list1 = role.getUserkeys();
		for(int i = 0; i < list.size(); i++){
			value += "'" + list.get(i) + "',";
		}
		value = value.substring(0,value.length()-1);
		for(int i = 0; i < list1.size(); i++){
			values += "'" + list1.get(i) + "',";
		}
		values = values.substring(0,values.length()-1);
		String sql = "UPDATE t_role tr,t_role_user tu SET tr.schoolcode=:schoolcode AND tr.code = :code AND tr.name = :name AND  tr.describe= :describe AND tr.remark = :remark WHERE tr.code = tu.rolecode AND tu.rolecode in (" + value + ") AND  tu.userkey in (" + values + ")";
		int a = getDao().executeSQL(sql,role);
		if(a == -1){
			flag = false;
		}
		return flag;
	}

	/**查看角色下面的用户
	 * @author:wuqiwei
	 * @param @param role
	 * @param @return
	 * @date:2016年2月23日下午2:31:30
	*/
	public Map getRoleUser(Role role) {
		Map map = new HashMap();
		String sql = "SELECT tu.id,tu.alias,tu.cellphone,tu.address,tu.imei,tu.schoolcode,tu.userkey,tu.islock,tu.headimg,tr.code FROM t_role tr,t_role_user tl,t_user tu WHERE tr.code = tl.rolecode AND tl.userkey = tu.userkey";
		if(role.getCode() != null && role.getCode().length() > 0){
			sql += " AND tr.code = :code";
		}
		if(role.getName() != null && role.getName().length() > 0){
			sql += " and tu.alias LIKE :name";
			role.setName("%"+role.getName()+"%");
		}
		String sql2 = "select count(*) from ("+ sql +") t";
		List<Object[]> list = getDao().queryBySqlPage(sql,role,role.getCurrentpage(),role.getPagesize()); 
		int counts = 0;
		counts = getDao().getCountBySqlPage(sql2);
		List<TUser>  result = new ArrayList<TUser>();
		for (Object[] obj :list) {
			TUser user = new TUser();
			user.setId(Long.valueOf(String.valueOf(obj[0])));
			user.setAlias(String.valueOf(obj[1]));
			user.setCellphone(String.valueOf(obj[2]));
			user.setAddress(String.valueOf(obj[3]));
			user.setImei(String.valueOf(obj[4]));
			user.setSchoolcode(String.valueOf(obj[5]));
			user.setUserkey(String.valueOf(obj[6]));
			user.setIslock(Integer.parseInt(String.valueOf(obj[7])) == 1 ? true :false);
			user.setHeadimg(String.valueOf(obj[8]));
			user.setCode(String.valueOf(obj[9]));
			result.add(user);
		}
		map.put("data", result);
		map.put("total", counts);
		return map;
	}

	/**删除角色用户信息
	 * @author:wuqiwei
	 * @param @param role
	 * @param @return
	 * @date:2016年2月23日下午3:16:20
	*/
	public boolean deleteRoleUser(Role role) {
		boolean flag = false;
		String[] table = new String[]{"t_role_department","t_role_user","t_license","t_role"};
		String sql = "delete from ";
		for(int i=0;i<table.length;i++){
			String sql2 = sql + table[i] + " where rolecode='"+role.getRolecodes()+"'";
			if(table[i]=="t_role"){
				sql2 = sql + table[i] + " where role='"+role.getRolecodes()+"'";
			}
			table[i]=sql2;
			sql2="";
		}
		if(getDao().deleteSQL(table)>0){
			flag = true;
		}
		return flag;
//		boolean flag = false;
//		String sql = " DELETE FROM t_role_user WHERE rolecode = :code AND userkey = :userkey";
//		int a = getDao().executeSQL(sql,role);
//		if(a >= 0){
//			flag = true;
//		}
//		return flag;
	}

	/**批量添加角色用户信息
	 * @author:wuqiwei
	 * @param @param role
	 * @param @return
	 * @date:2016年2月23日下午3:44:42
	*/
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public boolean insertRoleUser(List<Role> roles) {
		try {
			String sql = "select * from t_role_user where rolecode = :code";//传入的多个用户 code都是相同的 同一角色下的不同用户
			List<Object[]> list = getDao().queryBySQL(sql,roles.get(0)); 
			Map map = new HashMap();
			for (Object[] obj: list) {
				Role re = new Role();
				re.setUserkey(String.valueOf(obj[2]));
				map.put(String.valueOf(obj[2]), obj);//将userkey作为key 跟传入的userkey 用map获取 看看是不是获取相同的value
			 }
			for (int i = 0; i < roles.size(); i++) {
				Role ro = roles.get(i);
				String userkey = EncryptUtil.encryptMd5Password2(ro.getUsercode()+ro.getUsertype());
				String code = ro.getCode();
				if(map.get(userkey) == null){
					String sql1 =  " insert into t_role_user values(null,:code,:userkey)";
					ro.setUserkey(userkey);
					getDao().executeSQL(sql1,ro);
				}
			}
			return  true;
		} catch (Exception e) {
			return  false;
		}
		
	}

	/**批量添加权限 角色数据权限
	 * @author:wuqiwei
	 * @param @param dataRole
	 * @param @return
	 * @date:2016年3月1日下午3:17:25
	*/
	@SuppressWarnings("unchecked")
	public boolean batchAddDataRole(List<DataRole> dataRole) {
		boolean flag = false;
		String rolecode = dataRole.get(0).getRolecode();
		String dsql = "delete from t_data_role where rolecode='"+rolecode+"'";
		getDao().executeSQL(dsql);
		for (int i = 0; i < dataRole.size(); i++) {
			DataRole role = dataRole.get(i);
			String qsql = "select * from t_data_role where rolecode = :rolecode and classcode = :classcode and grade = :grade and classes = :classes";
			List<Object[]> list = getDao().queryBySQL(qsql,role); 
			if(list.size() > 0){
				String usql = "update t_data_role set rolecode = :rolecode and classcode = :classcode and grade = :grade and classes = :classes where rolecode = :rolecode and classcode = :classcode and grade = :grade and classes = :classes";
				getDao().executeSQL(usql,role);
			}else{
				String sql = "insert into t_data_role values(null,:rolecode,:classcode,:grade,:classes,:remark)";
				 int a = getDao().executeSQL(sql,role);
				 if(a>=0){
					 flag =true;
				 }
			}
		}
		return flag;
	}
	
	/**添加权限角色模块授权
	 * @author:wuqiwei
	 * @param @param dataRole
	 * @param @return
	 * @date:2016年3月2日上午10:09:51
	*/
	public boolean addLicenseRoleModels(List<License> dataRole) {
		boolean flag = false;
		License l = null;
		if(dataRole.size()>0){
			l = dataRole.get(0);
			String sqldel = "delete from t_license where rolecode = :rolecode";
			getDao().executeSQL(sqldel,l);
		}
		String sql = "insert into t_license values";
		for(int i = 0; i < dataRole.size(); i++){
			License li = dataRole.get(i);
			sql += "(null,'"+li.getRolecode()+"','"+li.getModular()+"'),";
		}
		sql = sql.substring(0,sql.length()-1);
		int a = getDao().executeSQL(sql);
		if(a>=0){
			flag =true;
		}
		return flag;
	}

	public boolean updateModels(List<Models> listmodels) {
		if(listmodels.size() > 0){
			String sqldel = "delete from t_models";
			getDao().executeSQL(sqldel);
			String sql = "insert into t_models values ";
			for (Models models : listmodels) {
				sql += "(NULL,'"+models.getValue()+"','"+models.getUrl()+"','"+models.getIcon()+"','"+models.getCode()+"'),";
			}
			sql = sql.substring(0, sql.length()-1);
			int a = getDao().executeSQL(sql);
			return a == -1 ? false : true;
		}else{
			return false;
		}
	}

	/**数据授权 显示班级 没处理数据  数据是全部返回
	 * @author:wuqiwei
	 * @param @param datarole
	 * @param @return
	 * @date:2016年6月6日下午2:16:49
	*/
	public List<DataRole> getZtreeDataRole(DataRole datarole) {
		String sql =" select * from t_data_role where id >= '1'";
		if(datarole.getRolecode() != null && datarole.getRolecode().length()>0){
			datarole.setRolecode(datarole.getRolecode().replace(",", "^"));
			sql += " and rolecode in (:rolecode)";
		}
		if(datarole.getClasscode() != null && datarole.getClasscode().length() >0){
			sql += " and classcode = :classcode";
		}
		if(datarole.getGrade() != null && datarole.getGrade().length() >0){
			sql += " and grade = :grade";
		}
		if(datarole.getClasses() != null && datarole.getClasses().length() >0){
			sql += " and classes = :classes";
		}
		if(datarole.getRemark() != null && datarole.getRemark().length() >0){
			sql += " and remark = :remark";
		}
		List<Object[]> list = getDao().queryBySQL(sql,datarole); 
		List<DataRole> result = new ArrayList<DataRole>();
		for(Object[] obj :list){
			DataRole role = new DataRole();
			role.setId(Long.valueOf(String.valueOf(obj[0])));
			role.setRolecode(String.valueOf(obj[1]));
			role.setClasscode(String.valueOf(obj[2]));
			role.setGrade(String.valueOf(obj[3]));
			role.setClasses(String.valueOf(obj[4]));
			role.setRemark(String.valueOf(obj[5]));
			result.add(role);
		}
		return result;
	}

}
